//
//  AcquisitionParametersTableViewCell.m
//  RTUApp
//
//  Created by 陈德军 on 2017/6/19.
//  Copyright © 2017年 陈德军. All rights reserved.
//


#import "AcquisitionParametersTableViewCell.h"
#import "AcquisitionParametersModel.h"

@interface AcquisitionParametersTableViewCell()<UITextFieldDelegate>{
    
}



@end

@implementation AcquisitionParametersTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+(instancetype)cellWithtableView:(UITableView *)tableview
{
    static NSString *ID = @"cell";
    AcquisitionParametersTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if(!cell)
    {
        cell = [[AcquisitionParametersTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
    
}

//重写布局
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.IDLabel = [UILabel new];
        self.IDLabel.textAlignment = NSTextAlignmentCenter;
        self.IDLabel.size = CGSizeMake(90, 23);
        self.IDLabel.left = 0;
        self.IDLabel.top = 0;
        [self.contentView addSubview:self.IDLabel];
        
        self.typeLabel = [UILabel new];
        self.typeLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *typeTGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTypeLabelEvent)];
        self.typeLabel.userInteractionEnabled = YES;
        [self.typeLabel addGestureRecognizer:typeTGR];
        self.typeLabel.size = CGSizeMake(90, 23);
        self.typeLabel.left = self.IDLabel.right;
        self.typeLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.typeLabel];
        
        self.enabledLabel = [UILabel new];
        self.enabledLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *enabledTGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickEnabledLabelEvent)];
        self.enabledLabel.userInteractionEnabled = YES;
        [self.enabledLabel addGestureRecognizer:enabledTGR];
        self.enabledLabel.size = CGSizeMake(90, 23);
        self.enabledLabel.left = self.typeLabel.right;
        self.enabledLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.enabledLabel];
        
        self.ElementTagLabel = [UILabel new];
        self.ElementTagLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *ElementTagTGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickElementTagLabelEvent)];
        self.ElementTagLabel.userInteractionEnabled = YES;
        [self.ElementTagLabel addGestureRecognizer:ElementTagTGR];
        self.ElementTagLabel.size = CGSizeMake(90, 23);
        self.ElementTagLabel.left = self.enabledLabel.right;
        self.ElementTagLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.ElementTagLabel];
        
        self.CollectionADDigitsLabel = [UITextField new];
        self.CollectionADDigitsLabel.textAlignment = NSTextAlignmentCenter;
        self.CollectionADDigitsLabel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.CollectionADDigitsLabel.delegate = self;
        self.CollectionADDigitsLabel.size = CGSizeMake(90, 23);
        self.CollectionADDigitsLabel.left = self.ElementTagLabel.right;
        self.CollectionADDigitsLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.CollectionADDigitsLabel];
        
        self.linearParameterLabel = [UITextField new];
        self.linearParameterLabel.textAlignment = NSTextAlignmentCenter;
        self.linearParameterLabel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.linearParameterLabel.delegate = self;
        self.linearParameterLabel.size = CGSizeMake(90, 23);
        self.linearParameterLabel.left = self.CollectionADDigitsLabel.right;
        self.linearParameterLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.linearParameterLabel];
        
        self.conversionFactorLabel = [UITextField new];
        self.conversionFactorLabel.textAlignment = NSTextAlignmentCenter;
        self.conversionFactorLabel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.conversionFactorLabel.delegate = self;
        self.conversionFactorLabel.size = CGSizeMake(90, 23);
        self.conversionFactorLabel.left = self.linearParameterLabel.right;
        self.conversionFactorLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.conversionFactorLabel];
        
        self.calibrationParametersLabel = [UITextField new];
        self.calibrationParametersLabel.textAlignment = NSTextAlignmentCenter;
        self.calibrationParametersLabel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.calibrationParametersLabel.delegate = self;
        self.calibrationParametersLabel.size = CGSizeMake(90, 23);
        self.calibrationParametersLabel.left = self.conversionFactorLabel.right;
        self.calibrationParametersLabel.top = self.IDLabel.top;
        [self.contentView addSubview:self.calibrationParametersLabel];
    }
    return self;
}

//给cell控件赋值
-(void)setModel:(AcquisitionParametersModel *)model{
    
    self.IDLabel.text = model.IDStr;
    
    if ([model.typeStr isEqualToString:@"1"]) {
        self.typeLabel.text = @"格雷码型";
    }else if ([model.typeStr isEqualToString:@"2"]){
        self.typeLabel.text = @"RS485型";
    }else if ([model.typeStr isEqualToString:@"3"]){
        self.typeLabel.text = @"开关型";
    }else if ([model.typeStr isEqualToString:@"4"]){
        self.typeLabel.text = @"电流型";
    }else if ([model.typeStr isEqualToString:@"5"]){
        self.typeLabel.text = @"电压型";
    }else{
        self.typeLabel.text = model.typeStr;
    }
    
    
    if ([model.enabledStr isEqualToString:@"0"]) {
        self.enabledLabel.text = @"否";
    }else{
        self.enabledLabel.text = @"是";
    }
    
    if ([model.ElementTagStr isEqualToString:@"0010"] || [model.ElementTagStr isEqualToString:@"1000"]) {
        self.ElementTagLabel.text = @"风速";
    }else if ([model.ElementTagStr isEqualToString:@"0020"] || [model.ElementTagStr isEqualToString:@"0020"]){
        self.ElementTagLabel.text = @"风向";
    }else if ([model.ElementTagStr isEqualToString:@"0001"] || [model.ElementTagStr isEqualToString:@"0100"]){
        self.ElementTagLabel.text = @"气压";
    }
    else if ([model.ElementTagStr isEqualToString:@"0004"] || [model.ElementTagStr isEqualToString:@"0400"]){
        self.ElementTagLabel.text = @"湿度";
    }
    else if ([model.ElementTagStr isEqualToString:@"0008"] || [model.ElementTagStr isEqualToString:@"0800"]){
        self.ElementTagLabel.text = @"气温";
    }
    else if ([model.ElementTagStr isEqualToString:@"0040"] || [model.ElementTagStr isEqualToString:@"4000"]){
        self.ElementTagLabel.text = @"蒸发量";
    }else if ([model.ElementTagStr isEqualToString:@"0708"] || [model.ElementTagStr isEqualToString:@"0807"]){
        self.ElementTagLabel.text = @"辐射";
    }else if ([model.ElementTagStr isEqualToString:@"0080"] || [model.ElementTagStr isEqualToString:@"8000"]){
        self.ElementTagLabel.text = @"降水量";
    }else if ([model.ElementTagStr isEqualToString:@"0101"] || [model.ElementTagStr isEqualToString:@"0101"]){
        self.ElementTagLabel.text = @"水位1";
    }else if ([model.ElementTagStr isEqualToString:@"0102"] || [model.ElementTagStr isEqualToString:@"0201"]){
        self.ElementTagLabel.text = @"水位2";
    }
    else if ([model.ElementTagStr isEqualToString:@"0201"] || [model.ElementTagStr isEqualToString:@"0102"]){
        self.ElementTagLabel.text = @"水压1";
    }else if ([model.ElementTagStr isEqualToString:@"0280"] || [model.ElementTagStr isEqualToString:@"8002"]){
        self.ElementTagLabel.text = @"埋深";
    }
    else if ([model.ElementTagStr isEqualToString:@"0501"] || [model.ElementTagStr isEqualToString:@"0105"]){
        self.ElementTagLabel.text = @"水温";
    }
    else if ([model.ElementTagStr isEqualToString:@"0401"] || [model.ElementTagStr isEqualToString:@"0104"]){
        self.ElementTagLabel.text = @"10CM墒情";
    }
    else if ([model.ElementTagStr isEqualToString:@"0402"] || [model.ElementTagStr isEqualToString:@"0204"]){
        self.ElementTagLabel.text = @"20CM墒情";
    }
    else if ([model.ElementTagStr isEqualToString:@"0408"] || [model.ElementTagStr isEqualToString:@"0804"]){
        self.ElementTagLabel.text = @"40CM墒情";
    }else if ([model.ElementTagStr isEqualToString:@"0420"] || [model.ElementTagStr isEqualToString:@"2004"]){
        self.ElementTagLabel.text = @"60CM墒情";
    }
    else if ([model.ElementTagStr isEqualToString:@"0210"] || [model.ElementTagStr isEqualToString:@"1002"]){
        self.ElementTagLabel.text = @"闸门开度";
    }
    else if ([model.ElementTagStr isEqualToString:@"0710"] || [model.ElementTagStr isEqualToString:@"1007"]){
        self.ElementTagLabel.text = @"电压";
    }else{
        self.ElementTagLabel.text = model.ElementTagStr;
    }
    self.CollectionADDigitsLabel.text = model.CollectionADDigitsStr;
    
    self.linearParameterLabel.text = model.linearParameterStr;
    
    self.conversionFactorLabel.text = model.conversionFactorStr;
    
    self.calibrationParametersLabel.text = model.calibrationParametersStr;
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField isEqual:self.CollectionADDigitsLabel]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickCollectionADDigitsLabelEvent: andValue:)]) {
            [self.delegate delegateClickCollectionADDigitsLabelEvent:self.IDLabel.text andValue:self.CollectionADDigitsLabel.text];
        }
    }else if ([textField isEqual:self.linearParameterLabel]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClicklinearParameterLabelEvent:andValue:)]) {
            [self.delegate delegateClicklinearParameterLabelEvent:self.IDLabel.text andValue:self.linearParameterLabel.text];
        }
    }else if ([textField isEqual:self.conversionFactorLabel]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickConversionFactorLabelEvent: andValue:)]) {
            [self.delegate delegateClickConversionFactorLabelEvent:self.IDLabel.text andValue:self.conversionFactorLabel.text];
        }
    }else if ([textField isEqual:self.calibrationParametersLabel]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickCalibrationParametersLabelEvent: andValue:)]) {
            [self.delegate delegateClickCalibrationParametersLabelEvent:self.IDLabel.text andValue:self.CollectionADDigitsLabel.text];
        }
    }else{
        
    }
}

#pragma mark - 按钮点击事件，通过代理模式响应
-(void)btnClick:(UIButton *)btn
{
    [self.delegate didClickButton:btn];
}

- (void)clickTypeLabelEvent{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickTypeLabelEvent:)]) {
        [self.delegate delegateClickTypeLabelEvent:self.IDLabel.text];
    }
}
- (void)clickEnabledLabelEvent{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickEnabledLabelEvent:)]) {
        [self.delegate delegateClickEnabledLabelEvent:self.IDLabel.text];
    }
}
- (void)clickElementTagLabelEvent{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickElementTagLabelEvent:)]) {
        [self.delegate delegateClickElementTagLabelEvent:self.IDLabel.text];
    }
}






@end
